11651번 좌표 정렬하기 2

Day 13 13단계 20231107

import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringBuilder sb = new StringBuilder();

		int n = Integer.parseInt(br.readLine());
		int[][] coord = new int[n][2];
		for (int i = 0; i < n; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine(), " ");
			coord[i][0] = Integer.parseInt(st.nextToken());
			coord[i][1] = Integer.parseInt(st.nextToken());
		}

		Arrays.sort(coord, (a, b) -> {
			if(a[1] != b[1]) { return a[1] - b[1]; }
			else if (a[0] != b[0]) { return a[0] - b[0]; }
			return a[1] - b[1];		
		});

		for (int[] arr : coord) {
			sb.append(arr[0] + " " + arr[1] + "\n");
		}

		System.out.println(sb.toString());
		bw.flush();
		bw.close();
		br.close();
	}
}